/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.viweb.storemanager.entity;

import com.viweb.storemanager.core.PropertyIndex;
import com.viweb.storemanager.core.SafeObject;
import java.text.Collator;
import java.text.RuleBasedCollator;
import java.util.Date;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author nhchung
 */
public class Employee extends BaseEntity implements Comparable, Cloneable, SafeObject, PropertyIndex {

    //khai báo biến
    protected Object cloner;
    private String name;
    private String address;
    private String phone;
    private Date startdate;
    private String username;
    private String password;
    private Rule rule;

    //khai báo hàm khởi tạo
    public Employee() {
    }

    public Employee(int id, String name) {
        setId(id);
        setName(name);
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the address
     */
    public String getAddress() {
        return address;
    }

    /**
     * @param address the address to set
     */
    public void setAddress(String address) {
        this.address = address;
    }

    /**
     * @return the phone
     */
    public String getPhone() {
        return phone;
    }

    /**
     * @param phone the phone to set
     */
    public void setPhone(String phone) {
        this.phone = phone;
    }

    /**
     * @return the startdate
     */
    public Date getStartdate() {
        return startdate;
    }

    /**
     * @param startdate the startdate to set
     */
    public void setStartdate(Date startdate) {
        this.startdate = startdate;
    }

    public String getUsername() {
        return username;
    }

    /**
     * @param username the username to set
     */
    public void setUsername(String username) {
        this.username = username;
    }

    /**
     * @return the password
     */
    public String getPassword() {
        return password;
    }

    /**
     * @param password the password to set
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * @return the rule
     */
    public Rule getRule() {
        return rule;
    }

    /**
     * @param rule the rule to set
     */
    public void setRule(Rule rule) {
        this.rule = rule;
    }

    //khai báo phương thức
    public boolean checkMail(String email) {
        Pattern pattern = Pattern.compile("^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+.[A-Za-z]");
        Matcher match = pattern.matcher(email);
        return match.matches();
    }

    public Object getCloner() {
        return this.cloner;
    }

    public boolean validate() {
        if (this.getRule() == null) {
            this.setValidDescription("Chưa chọn quyền cho nhân viên.");
            return false;
        }

        return true;
    }

    public Object getPropertyValue(int index) {
        String value = "";
        switch (index) {
            case 1:
                value = String.valueOf(getId());
                break;
            case 2:
                value = getName();
                break;
            case 3:
                value = getAddress();
                break;
            case 4:
                value = getPhone();
                break;
            case 5:
                value = getStartdate().toString();
                break;
            case 6:
                value = getUsername();
                break;
            case 7:
                value = getPassword();
                break;
            case 8:
                value = getRule().getRule();
                break;
            case 9:
                value = getDatemodifies().toString();
                break;
            case 10:
                value = String.valueOf(getStatus());
                break;
            case 11:
                value = getNote();
                break;
        }
        return value;
    }

    public void setPropertyValue(int index, Object value) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public Class getPropertyClass(int index) {
        return String.class;
    }

    public int compareTo(Object object) {
        if (!(object instanceof Employee)) {
            throw new ClassCastException("Ngoại lệ: đối tượng thức uống không hợp lệ!");
        }

        RuleBasedCollator viCollator = (RuleBasedCollator) Collator.getInstance(new Locale("vi"));
        return viCollator.compare(this.getName(), ((Employee) object).getName());

    }

    public void backup() {
        try {
            this.cloner = this.clone();
        } catch (CloneNotSupportedException ex) {
        }
    }

    public void restore() {
        this.setName(((Employee) getCloner()).getName());
        this.setPhone(((Employee) getCloner()).getPhone());
        this.setAddress(((Employee) getCloner()).getAddress());
        this.setUsername(((Employee) getCloner()).getUsername());
        this.setPassword(((Employee) getCloner()).getPassword());
        this.setRule(((Employee) getCloner()).getRule());
    }

    @Override
    public String toString() {
        return getName();
    }
}
